package TextNor.Initial;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import vn.hus.nlp.sd.SentenceDetector;
import GlobalVar.Constan;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author thắng
 */
public class sentenceDetector {//sử dụng bộ phân tách câu

    public void sentDet() throws XMLStreamException, IOException {
        Constan constan = new Constan();
        //<W NSW="NONE" FULL="XXX">﻿</W>
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        // XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            SentenceDetector test = new SentenceDetector(constan.SentenceDetectorModel);//load file model.bin vao mo hinh
            String[] str = test.detectSentences(constan.inputSentDetector);//dau vao de phan tach cau
            //System.out.println("so cau: " + str.length);
            fos = new FileOutputStream("textNor\\input_sentDetected.xml");
            osw = new OutputStreamWriter(fos, "UTF8");
            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
            writer.writeStartElement("root");
            writer.writeStartElement("record");
            writer.writeAttribute("recId", "1");
            writer.writeStartElement("Information");
            for (int i = 0; i < str.length; i++) {
                String string = str[i];
                int li = string.length();
                if (((string.endsWith(".") | string.endsWith(",") | string.endsWith(";") | string.endsWith("!") | string.endsWith("?")) && (!string.endsWith("...")))) {
                    // có dấu câu ở cuối thì tag dấu câu
                    writer.writeCharacters((String) string.subSequence(0, li - 1));
                    writer.writeStartElement("W");
                    writer.writeAttribute("NSW", "SENT");
                    writer.writeAttribute("FULL", "___");
                    writer.writeCharacters((String) string.substring(li - 1));
                    writer.writeEndElement();
                } else if (string.endsWith("...")) {
                    // có dâu câu là dấu ba chấm
                    int startDot = string.indexOf("...");
                    writer.writeCharacters((String) string.subSequence(0, startDot));
                    writer.writeStartElement("W");
                    writer.writeAttribute("NSW", "SENT");
                    writer.writeAttribute("FULL", "___");
                    writer.writeCharacters((String) string.substring(startDot));
                    writer.writeEndElement();
                } else {
                    //không có dấu câu thì thêm dấu câu
                    writer.writeCharacters(string);
                    writer.writeStartElement("W");
                    writer.writeAttribute("NSW", "SENT");
                    writer.writeAttribute("FULL", "___");
                    writer.writeCharacters(".");
                    writer.writeEndElement();
//                    System.out.println("da them dau cham");
                }
                System.out.println();
            }
            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndElement();
            writer.flush();
            writer.close();

            osw.close();
            fos.close();
        } catch (IOException ex) {
        }
// chưa viết xuống file


    }

    public static void main(String[] args) throws XMLStreamException, IOException {
        sentenceDetector obj = new sentenceDetector();
        obj.sentDet();
    }
}
